package com.dd;

import java.util.HashMap;
import java.util.Map;
import com.dd.exception.*;

import com.chantel.tool.db.DBUtil;

public class User {

	private int id;

	private String YongHuMing;

	private String MiMa;

	private String XingMing;

	private int DengJi;
	
	private String GET_USER = "select * from YuanGongInfoTable";

	public User(String yongHuMing, String miMa, String xingMing, int dengJi) {
		super();
		YongHuMing = yongHuMing;
		MiMa = miMa;
		XingMing = xingMing;
		DengJi = dengJi;
	}
	public User(int idin, String yongHuMing, String miMa, String xingMing, int dengJi) {
		id = idin;
		YongHuMing = yongHuMing;
		MiMa = miMa;
		XingMing = xingMing;
		DengJi = dengJi;
	}
	
	
	public User(int id)
	throws UserNotFoundException{
		this.id = id;
		try
		{
			loadFromDbByID(id);
		}
		catch(UserNotFoundException em)
		{
			throw em;
		}
		
	}
	
	public User(String userName)
	throws UserNotFoundException{
		this.YongHuMing  = userName;
		
		try
		{
			loadFromDbByName();
		}
		catch(UserNotFoundException em)
		{
			throw em;
		}
	}

	private void loadFromDbByID(int id)
	throws UserNotFoundException{
		// TODO 通过ID加载
		Map whereFields = new HashMap();
		whereFields.put("YuanGongId", new Integer(id));
		DBUtil2 db2 = DBUtil2.getDefaultDBUtil2();
		Map row = db2.getRow(GET_USER, whereFields);
		if(row == null)
		{
			throw new UserNotFoundException();
		}
		rowDateToFields(row);
	}
	
	//把取到的值传到各个属性上去
	private void rowDateToFields(Map row){
		this.id = ((Integer)row.get("YuanGongId")).intValue();
		this.DengJi = ( (Integer)row.get("DengJi") ).intValue();
		this.XingMing = (String)row.get("XingMing");
		this.YongHuMing = (String)row.get("YongHuMing");
		this.MiMa = (String)row.get("MiMa");
	}
	
	private void loadFromDbByName()
	throws UserNotFoundException{
		// TODO 通过Name加载
		Map whereFields = new HashMap();
		whereFields.put("YongHuMing", this.YongHuMing);
		DBUtil2 db2 = DBUtil2.getDefaultDBUtil2();
		Map row = db2.getRow(GET_USER, whereFields);
		
		if(row == null)
		{
			throw new UserNotFoundException();
		}
		rowDateToFields(row);
		
	}
	
	public boolean insertIntoDB(){
		DBUtil du = DBUtil.getInstance();
		Map m = new HashMap();
		this.id = SequenceManager.nextID(SysGlobals.USER_ID);
		m.put("YuanGongId", new Integer(this.id ));
		m.put("YongHuMing", this.YongHuMing);
		m.put("MiMa", this.MiMa);
		m.put("XingMing", this.XingMing);
		m.put("DengJi",new Integer(this.DengJi));
		
		boolean bool_res = du.insertRecord("YuanGongInfoTable", m);
		if (bool_res)
		{
			System.out.println("insert finish!");
			/*
			try
			{
				this.loadFromDbByName();	
			}
			catch(UserNotFoundException ex)
			{	
				return false;
			}
			*/
			
			
			return true;
		}
		else
		{
			System.out.println("insert error! s");
			return false;
		}		
	}
	
	public boolean deleteUser(){
		
		DBUtil du = DBUtil.getInstance();

		String sql = "delete from YuanGongInfoTable where YuanGongId = "
				+ ((Integer)(new Integer(this.id))).toString();

		boolean bool_res = du.deleteRecords(sql);

		if (bool_res)
		{
			System.out.println("delete user finish!");			
			
			return true;
		}
		else
		{
			System.out.println("delete user error! s");
			return false;
		}	
		
	}
	
	//
	//使用ID来更新数据
	//
	public boolean updateTODB(){
		/*
		String strCommand = "UPDATE YuanGongInfoTable " +
							"SET YuanGongId = " + ((Integer)(new Integer(this.id))).toString() +
							", YongHuMing = "+ this.YongHuMing +
							", MiMa = " + this.MiMa + 
							",XingMing = " + this.XingMing +
							",DengJi = " + ((Integer)(new Integer(this.DengJi))).toString()  +
							"WHERE (YuanGongId = " + ((Integer)(new Integer(this.id))).toString() +
							")";
		*/
		
		DBUtil du = DBUtil.getInstance();
		Map m = new HashMap();
		
		m.put("YuanGongId", (Integer)(new Integer(this.id)));
		m.put("YongHuMing", this.YongHuMing);
		m.put("MiMa", this.MiMa);
		m.put("XingMing",this.XingMing);
		m.put("DengJi", (Integer)(new Integer(this.DengJi)));

		String updateWhere = "YuanGongId = " +  ((Integer)(new Integer(this.id))).toString();
		
		boolean bool_res = du.updateFieldsValue("YuanGongInfoTable", m,updateWhere);
		
		return bool_res;
		
	}

	public int getDengJi() {
		return DengJi;
	}

	public void setDengJi(int dengJi) {
		DengJi = dengJi;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getMiMa() {
		return MiMa;
	}

	public void setMiMa(String miMa) {
		MiMa = miMa;
	}

	public String getXingMing() {
		return XingMing;
	}

	public void setXingMing(String xingMing) {
		XingMing = xingMing;
	}

	public String getYongHuMing() {
		return YongHuMing;
	}

	public void setYongHuMing(String yongHuMing) {
		YongHuMing = yongHuMing;
	}
	
	//是否属于系统管理员
	public boolean isSysAdmin(){
		if (this.DengJi == SysGlobals.C_Admin)
			return true;
		else 
			return false;
	}
	
	//是否属于采购员
	public boolean isCaiGouYuan(){
		if (this.DengJi == SysGlobals.C_CaiGouYuan)
			return true;
		else 
			return false;
	}
	
	//是否属于审核员
	public boolean isShenHeYuan(){
		if (this.DengJi == SysGlobals.C_ShenHeYuan)
			return true;
		else 
			return false;		
	}
	
	//审核员是否属于项目组
	public boolean isXiangMuZu(){
		if (!isShenHeYuan())
			return false;
		try {
			int nUserQuanXian = UserManager.GetShenHeYuanQuanXianByUserID(this.getYongHuMing());
			if (nUserQuanXian == SysGlobals.C_ShenHe_XiangMuZu)
				return true;
		
		} catch (UserNotFoundException e) {
			e.printStackTrace();
			return false;
		}
		return false;
	}
	
	//审核员是否属于采购部
	public boolean isCaiGouBu(){
		if (!isShenHeYuan())
			return false;
		try {
			int nUserQuanXian = UserManager.GetShenHeYuanQuanXianByUserID(this.getYongHuMing());
			if (nUserQuanXian == SysGlobals.C_ShenHe_CaiGouBu)
				return true;
		
		} catch (UserNotFoundException e) {
			e.printStackTrace();
			return false;
		}
		return false;
	}
	
	//审核员是否属于比价部
	public boolean isBiJiaBu(){
		if (!isShenHeYuan())
			return false;
		try {
			int nUserQuanXian = UserManager.GetShenHeYuanQuanXianByUserID(this.getYongHuMing());
			if (nUserQuanXian == SysGlobals.C_ShenHe_BiJiaBu)
				return true;
		
		} catch (UserNotFoundException e) {
			e.printStackTrace();
			return false;
		}
		return false;
	}
	
	//审核员是否属于经理
	public boolean isJingLi(){
		if (!isShenHeYuan())
			return false;
		try {
			int nUserQuanXian = UserManager.GetShenHeYuanQuanXianByUserID(this.getYongHuMing());
			if (nUserQuanXian == SysGlobals.C_ShenHe_JingLi)
				return true;
		
		} catch (UserNotFoundException e) {
			e.printStackTrace();
			return false;
		}
		return false;
	}
	
	//审核员是否属于总裁
	public boolean isZhongCai(){
		if (!isShenHeYuan())
			return false;
		try {
			int nUserQuanXian = UserManager.GetShenHeYuanQuanXianByUserID(this.getYongHuMing());
			if (nUserQuanXian == SysGlobals.C_ShenHe_ZhongCai)
				return true;
		
		} catch (UserNotFoundException e) {
			e.printStackTrace();
			return false;
		}
		return false;
	}
	
	public static void main(String[] args){
		
		try
		{
			User user = new User("admin");	
			System.out.println(user.MiMa);
		}
		catch(UserNotFoundException em)
		{
			
		}
		
	}

	
}
